package design.pattern.Lu22FlyweightPattern;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2020-05-19 20:48:00
 */
public class FlyweightPattern02 {

    public void tets01(){

    }

    public static void main(String[] args) {
        //初始化对象池
        for(int i=0;i<4;i++){
            String subject = "科目" + i; //初始化地址
            for(int j=0;j<30;j++){
                String key = subject + "考试地点"+j;
                SignInfo signInfo = SignInfoFactory.getSignInfo(key);
                System.out.println(signInfo.toString());
            }
        }
        SignInfo signInfo = SignInfoFactory.getSignInfo("科目1考试地点1");
        System.out.println(signInfo.toString());
    }

}

@Data
class SignInfo{
    private String id;
    private String location;
    private String subject;
    private String address;
}

@Data
class SignInfoPool extends SignInfo{
    private String key;
    public SignInfoPool(String key){
        this.key = key;
    }
}

class SignInfoFactory{

    private static Map<String, SignInfo> pool = new HashMap<>();

    @Deprecated
    public static SignInfo getSignInfo(){
        return new SignInfo();
    }

    public static SignInfo getSignInfo(String key){
        SignInfo signInfo = null;
        if(pool.containsKey(key)){
            signInfo = pool.get(key);
        }else {
            signInfo = new SignInfoPool(key);
            pool.put(key, signInfo);
        }
        return signInfo;
    }

}
